package com.example.musicplayer.calculator;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.BaseActivity;
import com.example.musicplayer.commonUtils.DisplayUtil;
import com.example.musicplayer.windowTools.WindowTools;

public class CalculatorActivity extends BaseActivity implements View.OnClickListener {
    public static final String CALCULATOR_INTENT_ACTION = "android.intent.action.MUSIC_PLAYER_CALCULATOR";
    private static final int LENGTH_LIMIT_1 = 10;
    private static final int LENGTH_LIMIT_2 = 14;
    private static final int LENGTH_LIMIT_3 = 20;
    private EditText mInput;
    private TextView mOutput;
    private String mExp;
    private boolean mNeedCalculate = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        WindowTools.lightStatusBar(getWindow());
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mInput.requestFocus();
    }

    private void init() {
        mInput = findViewById(R.id.cal_input);
        mOutput = findViewById(R.id.cal_output);
        DisplayUtil.disableSoftInput(mInput);
        mExp = "";
        mInput.setText(mExp);
        mOutput.setText(mExp);
        mInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mNeedCalculate = true;
                updateInputTextSize();
            }
        });
    }

    private void updateInputTextSize() {
        int sizeId;
        if (mExp.length() <= LENGTH_LIMIT_1) {
            sizeId = R.dimen.cal_input_size_1;
        } else if (mExp.length() <= LENGTH_LIMIT_2) {
            sizeId = R.dimen.cal_input_size_2;
        } else if (mExp.length() <= LENGTH_LIMIT_3) {
            sizeId = R.dimen.cal_input_size_3;
        } else {
            sizeId = R.dimen.cal_input_size_4;
        }
        int sizePixel = getResources().getDimensionPixelSize(sizeId);
        mInput.setTextSize(DisplayUtil.px2sp(this, sizePixel));
    }

    private void executeCalculate() {
        if (mNeedCalculate) {
            String result = "";
            try {
                result = Calculate.calculate(mExp);
            } catch (CalculateException e) {
                e.printStackTrace();
                result = e.getMessage();
            }
            mOutput.setText(result);
            mNeedCalculate = false;
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cal_0:
            case R.id.cal_1:
            case R.id.cal_2:
            case R.id.cal_3:
            case R.id.cal_4:
            case R.id.cal_5:
            case R.id.cal_6:
            case R.id.cal_7:
            case R.id.cal_8:
            case R.id.cal_9:
            case R.id.cal_point:
            case R.id.cal_add:
            case R.id.cal_subtract:
            case R.id.cal_multiply:
            case R.id.cal_divide:
            case R.id.cal_factorial:
            case R.id.cal_power:
            case R.id.cal_percent:
            case R.id.cal_left_bracket:
            case R.id.cal_right_bracket:
                if (v instanceof Button) {
                    String string = (String) ((Button) v).getText();
                    int position = mInput.getSelectionStart();
                    if (position == mExp.length()) {
                        mExp += string;
                    } else {
                        mExp = mExp.substring(0, position) + string + mExp.substring(position);
                    }
                    mInput.setText(mExp);
                    mInput.setSelection(position + 1);
                }
                break;
            case R.id.cal_delete:
                int position = mInput.getSelectionStart();
                if (position > 0 && mExp.length() > 0) {
                    if (position == mExp.length()) {
                        mExp = mExp.substring(0, mExp.length() - 1);
                    } else {
                        mExp = mExp.substring(0, position - 1) + mExp.substring(position);
                    }
                    mInput.setText(mExp);
                    mInput.setSelection(position - 1);
                }
                break;
            case R.id.cal_clear:
                mExp = "";
                mOutput.setText(mExp);
                mInput.setText(mExp);
                mInput.setSelection(0);
                break;
            case R.id.cal_equal:
                executeCalculate();
                break;
            default:
                break;
        }
    }
}